package com.rayboo.example.collections.list;

import java.util.LinkedList;
import java.util.Queue;

/**
 * LinkedList 示例
 *
 * @author ray
 * @since 2019/07/25
 *
 * <p>与Java中的数组类似，LinkedList是一种线性数据结构。
 * 但是，LinkedList元素不存储在像数组这样的连续位置中，它们使用指针相互链接。
 * LinkedList的每个元素都具有指向LinkedList的下一个元素的引用（地址/指针）。
 *
 * <p>https://beginnersbook.com/2013/12/linkedlist-in-java-with-example/
 */
public class LinkedListExample {

    public static void main(String args[]) {
        System.out.println("======= LinkedList =======");
        /* Linked List Declaration */
        LinkedList<String> linkedList = new LinkedList<String>();

        /*add(String Element) is used for adding
         * the elements to the linked list*/
        linkedList.add("Item1");
        linkedList.add("Item5");
        linkedList.add("Item3");
        linkedList.add("Item6");
        linkedList.add("Item2");

        /*Display Linked List Content*/
        System.out.println("Linked List Content: " + linkedList);

        /*Add First and Last Element*/
        linkedList.addFirst("First Item");
        linkedList.addLast("Last Item");
        System.out.println("LinkedList Content after addition: " + linkedList);

        /*This is how to get and set Values*/
        Object firstVar = linkedList.get(0);
        System.out.println("First element: " + firstVar);
        linkedList.set(0, "Changed first item");
        Object firstVar2 = linkedList.get(0);
        System.out.println("First element after update by set method: " + firstVar2);

        /*Remove first and last element*/
        linkedList.removeFirst();
        linkedList.removeLast();
        System.out.println("LinkedList after deletion of first and last element: " + linkedList);

        /* Add to a Position and remove from a position*/
        linkedList.add(0, "Newly added item");
        linkedList.remove(2);
        System.out.println("Final Content: " + linkedList);


        // Queue - LinkedList
        System.out.println("\n====== Queue - LinkedList ======");
        /*
         * We cannot create instance of a Queue as it is an
         * interface, we can create instance of LinkedList or
         * PriorityQueue and assign it to Queue
         */
        Queue<String> queue = new LinkedList<String>();

        //Adding elements to the Queue
        queue.add("Rick");
        queue.add("Maggie");
        queue.add("Glenn");
        queue.add("Negan");
        queue.add("Daryl");

        System.out.println("Elements in Queue:" + queue);

        /*
         * We can remove element from Queue using remove() method,
         * this would remove the first element from the Queue
         */
        System.out.println("Removed element: " + queue.remove());

        /*
         * element() method - this returns the head of the
         * Queue. Head is the first element of Queue
         */
        System.out.println("Head: " + queue.element());

        /*
         * poll() method - this removes and returns the
         * head of the Queue. Returns null if the Queue is empty
         */
        System.out.println("poll(): " + queue.poll());

        /*
         * peek() method - it works same as element() method,
         * however it returns null if the Queue is empty
         */
        System.out.println("peek(): " + queue.peek());

        //Again displaying the elements of Queue
        System.out.println("Elements in Queue:" + queue);
    }
}
